<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Process extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		echo "Process .php";
		$query=$this->db->get('tenant');

		foreach ($query->result() as $row)
		{
			    echo $row->company_name;
		}
	}

	public function tenant_register()
	{
		$tenantdata['OrgName']=$this->input->post("name",TRUE);
		$tenantdata['OrgUrl']=$this->input->post("url",TRUE);
		$tenantdata['email']=$this->input->post("email",TRUE);
		$tenantdata['phone']=$this->input->post("phone",TRUE);
		$tenantdata['username']=$this->input->post("username",TRUE);
		$tenantdata['password']=md5($this->input->post("password",TRUE));

		$this->db->insert('Organisation',$tenantdata);
		$result=$this->db->affected_rows();
		
		log_message('debug', 'Register tenant :: affected rows'.$result);
		
		if( $result > 0){
			echo 1;
		}else{
			echo 0;
		}

	}

	public function tenant_login()
	{
		$username=$this->input->post("username",TRUE);
	
		$password=$this->input->post("password",TRUE); //Apply md5 later.

		$query=$this->db->select('OrgID,OrgName,OrgUrl,email,phone')->from('organisation')->where('username',$username)->where('password',$password)->get();

		$row_count=count($query->result_array());
		
		log_message('debug', 'Tenant login :: affected rows'.$row_count);
		
		if( $row_count > 0 )
		{

			$row=$query->row();

			$suserdata['OrgID']=$row->OrgID;
			$suserdata['username']=$username;
			$suserdata['OrgName']=$row->OrgName;
			$suserdata['OrgUrl']=$row->OrgUrl;
			$suserdata['email']=$row->email;
			$suserdata['phone']=$row->phone;

			$this->session->set_userdata($suserdata);

			echo 1;
		}else{

			echo 0;
		}



	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
